<template>
	<view>
		<scroll-view scroll-y class="container authorization">
			<view class="box">
				<view>
					<text class="title">需要您的授权\n\n</text>
					<text class="msg" style="font-weight: 700;">仅限康健驿站内部观察员使用；\n</text>
					<text class="msg">请在稍后的提示框中点击“允许”,进行身份验证\n</text>
				</view>
				<image mode="widthFix" src="../../static/login.png"></image>
				<button type="primary" class="authBtn" open-type="getPhoneNumber"
					@getphonenumber="getPhoneNumber">立即授权登录</button>
				<button class="backBtn" @click="dialogToggle('success')">暂不授权</button>
			</view>
		</scroll-view>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog :type="msgType" cancelText="关闭" confirmText="同意" title="通知" content="为不影响下程序正常使用,请进行授权"
				@confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getPhoneApi,
		getTokenApi
	} from '../../common/api/user.js'
	import {
		mapMutations
	} from 'vuex'
	var WXBizDataCrypt = require('../../utils/WXBizDataCrypt')

	export default {
		data() {
			return {
				userLogin: {},
				msgType: ''
			};
		},
		onLoad() {
			this.handlecLogin()
		},
		methods: {
			...mapMutations('user', ['MugetUserInfo', 'MusetToken']),
			async getPhoneNumber(res) {
				//手机号解码
				var pc = new WXBizDataCrypt('wx8b4940163045b972', this.userLogin.sessionKey)
				var data = pc.decryptData(res.detail.encryptedData, res.detail.iv)
				const r2 = await getTokenApi(this.userLogin.openId, data.phoneNumber)
				this.MusetToken(r2.data.accessToken)
				//授权成功
				uni.switchTab({
					url: '/pages/home/home'
				})
			},
			async handlecLogin() {
				const res = await wx.login()
				const r = await getPhoneApi(res.code)
				console.log(res, r)
				this.userLogin = r.data
			},
			dialogToggle(type) {
				this.msgType = type
				this.$refs.alertDialog.open()
			},
			dialogConfirm(res) {
				console.log(res)
			},
			dialogClose(res) {
				console.log(res, 111)
			},

		}
	}
</script>

<style lang="scss">
	.box {
		text-align: center;
		margin-top: 50rpx;
	}

	.authBtn {
		margin: 20rpx 0;
	}

	.container {
		margin-top: 50rpx;
	}
</style>
